<?php
namespace Weixin\Controller;
use Think\Controller;
class MainController extends Controller{
	//初始化
	private $fromUserName= "";//获取发送方帐号(OpenID)
	private $toUserName= "";//开发者微信号
	private $content = "";//消息内容
	private $event = "";//事件类型
	private $eventKey = '';//事件key值
	private $msgType = '';//消息类型

	//验证token
	public function wxgettoken(){
		define("TOKEN", 'wxtools');
		define("DEBUG", true);
		$weixin = new \Util\WxMenu(TOKEN,DEBUG);//实例化
		file_put_contents('get.txt',var_export($_GET));
		if (!isset($_GET['echostr'])){
		    $weixin->getMsg();
		}else{
		    $weixin->valid($_GET);
		}
		/**************************************/
		// 用户发过来的数据
		/**************************************/
		$this->initData($weixin);
		//关注
		if($this->event == 'subscribe'){
			$result = "尊敬的客户您好，感谢关注“车贷闲”！【个人中心/绑定】获取更多优惠信息，轻松购车，省心又省钱！最新推出【0元购车】，更多详情请点击菜单栏。客服电话0851-85974912";
			file_put_contents('get.txt',json_encode($weixin->msg));
			$weixin->makeText($result); 
			//$this->subscribeEvent();
		}elseif($this->event == "SCAN"){
			$result = "扫码成功";
			file_put_contents('get.txt',json_encode($weixin->msg));
			$weixin->makeText($result); 
			//$this->scanEvent();
		}elseif($this->msgType == 'text'){
			//$this->autoReply($weixin);
		}	
	}

	//初始化数据
	private function initData($weixin){
		$this->fromUserName = $weixin->msg['FromUserName'];//获取发送方帐号（OpenID）
		$this->toUserName = $weixin->msg['ToUserName'];//开发者微信号
		$this->content = $weixin->msg['Content'];//消息内容
		$this->event = $weixin->msg['Event'];//事件类型
		$this->eventKey = $weixin->msg['EventKey'];//事件key值
		$this->msgType = $weixin->msg['MsgType'];//消息类型
	}
}